﻿using Core.Identity.Menus.Features.Commands.BatchDelete;
using Core.Identity.Menus.Features.Commands.Clone;
using Core.Identity.Menus.Features.Commands.Create;
using Core.Identity.Menus.Features.Commands.Update;
using Core.Identity.Menus.Features.Queries.GetAll;
using Core.Identity.Menus.Features.Queries.GetMe;
using Core.Interfaces.Users;
using Infrastructure.Auth.Policy;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;

namespace Infrastructure.Identity.Menus.Endpoints;

public static class MenuEndpoints
{
    public static IEndpointRouteBuilder MapMenuEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/api/menu")
            .WithTags("Menu")
            .WithDescription("菜单管理");

        // -----------------------
        // 获取全部菜单
        // GET /api/menu
        // -----------------------
        group.MapGet("/", async ([FromServices] IMediator mediator) =>
            await mediator.Send(new GetAllMenusQuery()))
            .RequirePermission("Menu:View:All")
            .WithDescription("查询全部菜单")
            .WithMetadata(new EndpointNameMetadata(nameof(GetAllMenusQuery)));

        // -----------------------
        // 创建菜单
        // POST /api/menu
        // -----------------------
        group.MapPost("/", async (
            [FromBody] CreateMenuCommand request,
            [FromServices] IMediator mediator) =>
            await mediator.Send(request))
            .RequirePermission("Menu:Create")
            .WithDescription("创建菜单")
            .WithMetadata(new EndpointNameMetadata(nameof(CreateMenuCommand)));

        // -----------------------
        // 修改菜单
        // PUT /api/menu/{id}
        // -----------------------
        group.MapPut("/{id:guid}", async (
            [FromBody] UpdateMenuCommand request,
            [FromServices] IMediator mediator) =>
            await mediator.Send(request))
            .RequirePermission("Menu:Update")
            .WithDescription("修改菜单")
            .WithMetadata(new EndpointNameMetadata(nameof(UpdateMenuCommand)));

        // -----------------------
        // 批量删除菜单
        // DELETE /api/menu
        // -----------------------
        group.MapDelete("/", async (
            [FromBody] BatchDeleteMenuCommand request,
            [FromServices] IMediator mediator) =>
            await mediator.Send(request))
            .RequirePermission("Menu:Delete")
            .WithDescription("批量删除菜单")
            .WithMetadata(new EndpointNameMetadata(nameof(BatchDeleteMenuCommand)));

        // -----------------------
        // 获取当前用户菜单
        // GET /api/menu/me
        // -----------------------
        group.MapGet("/me", async (
            [FromServices] ICurrentUserService currentUserService,
            [FromServices] IMediator mediator) =>
            await mediator.Send(new GetMeMenusQuery
            {
                UserId = currentUserService.GetUserId()
            }))
            .WithDescription("获取当前用户菜单")
            .WithMetadata(new EndpointNameMetadata("GetMeMenus"));

        // -----------------------
        // 克隆菜单
        // POST /api/menu/clone
        // -----------------------
        group.MapPost("/clone", async ([FromServices] IMediator mediator) =>
            await mediator.Send(new CloneMenuCommand()))
            .RequirePermission("Menu:Clone")
            .WithDescription("克隆菜单")
            .WithMetadata(new EndpointNameMetadata(nameof(CloneMenuCommand)));

        return app;
    }
}
